package com.calvin.study.scheduled;

import java.util.concurrent.TimeUnit;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.calvin.study.service.ISeckillService;

import lombok.extern.slf4j.Slf4j;

/**
 * 秒杀商品上架
 * @date:2022年8月2日 上午11:10:04
 */
@Slf4j
@Component
@EnableScheduling
public class SeckillProductSchedule {

	@Autowired
	private ISeckillService seckillService;
    @Autowired
    RedissonClient redissonClient;

    private final String SECKILL_PRODUCT_SHELVES_LOCK  = "SECKILL:PRODUCT:SHELVES:LOCK";
	
	//@Scheduled(cron="0 * * * * ?")//每分钟执行一次
	//@Scheduled(cron="*/3 * * * * ?")
	public void seckillProductShelves() {
		log.info("上架秒杀商品信息...");
		RLock lock = redissonClient.getLock(SECKILL_PRODUCT_SHELVES_LOCK);
		lock.lock(10, TimeUnit.SECONDS);
		try {
			seckillService.seckillProductShelves(3);
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			lock.unlock();
		}
	}
}
